package liumengbaio.lolzhangyoubao.Activity.utils;

import android.content.Context;

import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.InFo.AllHeroInfo;

/**
 * Created by lanouhn on 16/8/29.
 */
public class AllHeroUtils {
    /**
     * 全部英雄的获取方法
     */

    public static List<AllHeroInfo> getAllHerosList(Context context) {
        List<AllHeroInfo> ahList = new ArrayList<>();
        for (int i = 1; i < 133; i++) {

            int type = 133 - i;
            if (type >= 130) {
                AllHeroInfo nfhinfo = getAllHeroInfo(context, String.valueOf(type + 1));
                ahList.add(nfhinfo);
            } else {
                AllHeroInfo nfhinfo = getAllHeroInfo(context, String.valueOf(type));
                ahList.add(nfhinfo);
            }
        }
        return ahList;
    }

    private static AllHeroInfo getAllHeroInfo(Context context, String id) {
        AllHeroInfo nfhinfo = new AllHeroInfo();
        try {
            InputStream is = context.getAssets().open("hero/" + id + ".json");
            // Read the entire asset into a local byte buffer.
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            is.close();

            // Convert the buffer into a string.
            String text = new String(buffer, "UTF-8");

            JSONObject hJson = new JSONObject(text);

            nfhinfo.setNickname(hJson.getString("nickname"));
            nfhinfo.setPic_url(hJson.getString("pic_url"));
            nfhinfo.setMoney(hJson.getString("money"));
            nfhinfo.setPoint(hJson.getString("point"));
            nfhinfo.setId(hJson.getString("id"));
            nfhinfo.setFilter(hJson.getString("filter"));
            nfhinfo.setName(hJson.getString("name"));
            nfhinfo.setBase_physical(hJson.getString("base_physical"));
            nfhinfo.setBase_magic(hJson.getString("base_magic"));
            nfhinfo.setBase_blood(hJson.getString("base_blood"));
            nfhinfo.setBase_difficulty(hJson.getString("base_difficulty"));
            nfhinfo.setMoving_speed(hJson.getString("moving_speed"));
            nfhinfo.setRange(hJson.getString("range"));
            nfhinfo.setAttack(hJson.getString("attack"));
            nfhinfo.setAttack_speed(hJson.getString("attack_speed"));
            nfhinfo.setArmor(hJson.getString("armor"));
            nfhinfo.setBlood(hJson.getString("blood"));
            nfhinfo.setMana(hJson.getString("mana"));
            nfhinfo.setMagic_resistance(hJson.getString("magic_resistance"));
            nfhinfo.setBlood_recovery(hJson.getString("blood_recovery"));
            nfhinfo.setMagic_recovery(hJson.getString("magic_recovery"));
            nfhinfo.setUsetip(hJson.getString("usetip"));
            nfhinfo.setKilltip(hJson.getString("killtip"));
            nfhinfo.setBg(hJson.getString("bg"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return nfhinfo;

    }
}
